<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="档案名" prop="archivesName">
        <el-input
          v-model="queryParams.archivesName"
          placeholder="请输入"
          clearable
          style="width: 260px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案类型" prop="archivesType">
        <el-select
          v-model="queryParams.archivesType"
          placeholder="请选择"
          clearable
          style="width: 260px"
        >
          <el-option
            v-for="{ label, value } in sys_archives_type"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="archivesCharger">
        <el-input
          v-model="queryParams.archivesCharger"
          placeholder="请输入"
          clearable
          style="width: 260px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存放位置" prop="archivesLocation">
        <el-select
          v-model="queryParams.archivesLocation"
          placeholder="请选择"
          clearable
          style="width: 260px"
        >
          <el-option
            v-for="{ label, value } in sys_archives_location"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归档日期" style="width: 360px">
        <el-date-picker
          v-model="dateRange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item style="margin-left: 100px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['operation:archives:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['operation:archives:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:archives:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:archives:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operation:archives:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="archivesList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="档案名" align="center" prop="archivesName" />
      <el-table-column label="档案类型" align="center" prop="archivesType">
        <template #default="{ row }">
          <dict-tag :options="sys_archives_type" :value="row.archivesType" />
        </template>
      </el-table-column>
      <el-table-column
        label="归档日期"
        width="100"
        align="center"
        prop="archivesDate"
      />
      <el-table-column label="负责人" align="center" prop="archivesCharger" />
      <el-table-column label="存放位置" align="center" prop="archivesLocation"
        ><template #default="{ row }">
          <dict-tag
            :options="sys_archives_location"
            :value="row.archivesLocation"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            type="success"
            size="small"
            v-hasPermi="['operation:archives:borrow']"
            >借阅</el-button
          >
          <el-button
            type="warning"
            size="small"
            v-hasPermi="['operation:archives:exchange']"
            >交换</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="详情"
        align="center"
        class-name="small-padding  fixed-width"
      >
        <template #default="{ row }">
          <el-button
            type="text"
            icon="View"
            @click="handleView(row)"
            v-hasPermi="['operation:archives:detail']"
            >详细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        :model="form"
        :rules="rules"
        ref="archivesRef"
        label-width="90px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="档案名" prop="archivesName">
              <el-input
                v-model="form.archivesName"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="负责人" prop="archivesCharger">
              <el-input
                v-model="form.archivesCharger"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="档案类型" prop="archivesType">
              <el-select
                v-model="form.archivesType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="{ label, value } in sys_archives_type"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放位置" prop="archivesLocation">
              <el-select
                v-model="form.archivesLocation"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="{ label, value } in sys_archives_location"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归档日期" prop="archivesDate">
              <el-date-picker
                v-model="form.archivesDate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 档案详细 -->
    <el-dialog title="档案详细" v-model="openView" width="500px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="档案名：">{{
              form.archivesName
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="档案类型：">
              <dict-tag
                :options="sys_archives_type"
                :value="form.archivesType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归档日期：">{{
              form.archivesDate
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人：">
              {{ form.archivesCharger }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放位置：">
              <dict-tag
                :options="sys_archives_location"
                :value="form.archivesLocation"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="archives">
import {
  listArchives,
  getArchives,
  delArchives,
  addArchives,
  updateArchives
} from '@/api/operation/archives'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sys_archives_type } = proxy.useDict('sys_archives_type') //档案类型
const { sys_archives_location } = proxy.useDict('sys_archives_location') //档案存放位置

const archivesList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const openView = ref(false)
const title = ref('')
const total = ref(0)
const dateRange = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    archivesName: [
      { required: true, message: '档案名不能为空', trigger: 'blur' }
    ],
    archivesCharger: [
      { required: true, message: '负责人不能为空', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询养护内容列表 */
function getList() {
  loading.value = true
  const [archivesStartDate, archivesEndDate] = dateRange.value ?? []
  listArchives({
    ...queryParams.value,
    archivesStartDate,
    archivesEndDate
  }).then((response) => {
    archivesList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('archivesRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加档案信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const archivesId = row.id || ids.value
  getArchives(archivesId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改档案信息'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const archivesIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delArchives(archivesIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'operations/archives/export',
    {
      ...queryParams.value
    },
    `archives_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['archivesRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateArchives(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addArchives(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection
    .map((item) => +item.index)
    .sort(function (a, b) {
      return a - b
    })
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 初始化列表
getList()
</script>
